package com.stone.design.patterns.singleton;

/**
 * @author Stone
 * @version V1.0
 * @description: 利用同步解决多线程实例化产生多个对象的问题，但是会降低性能
 *          一旦mChocolateBoiler后 就不需同步了，但是同步还是每次实例化的时候都会用到
 * @date 4/12/18 21:01
 * @email redsstone@163.com
 */
public class ChocolateBoiler {

    private boolean empty;
    private boolean boiler;

    private static ChocolateBoiler mChocolateBoiler;
    private ChocolateBoiler() {
        empty = true;
        boiler = false;
    }

    public static synchronized ChocolateBoiler getInstance() {
        if (mChocolateBoiler == null) {
            mChocolateBoiler = new ChocolateBoiler();
        }
        return mChocolateBoiler;
    }

    /**
     * 锅炉填料
     */
    public void fill() {
        if (isEmpty()) {
            empty = false;
            boiler = false;
        }
    }

    public void drain() {
        if (!isEmpty() && isBoiler()) {
            empty = true;
        }
    }

    public void boil() {
        if (!isEmpty() && !isBoiler()) {
            boiler = true;
        }
    }

    public boolean isEmpty() {
        return empty;
    }

    public boolean isBoiler() {
        return boiler;
    }
}
